#include "StaticMesh.h"
#include "Textures\Texture.h"
#include "Textures\2DTexture.h"
#include "GraphicsManager.h"
#include "RenderableVertexs\RenderableVertexs.h"
#include <Windows.h>
#include <vector>
#include <stdio.h>
//-----Define new  vertex types-----------:

//-------------------------------------------------


CStaticMesh::CStaticMesh()
{
	m_NumVertexs = 0;
	m_NumFaces = 0;
}
CStaticMesh::~CStaticMesh()
{
}
bool CStaticMesh::Load(const std::string &FileName)
{
	FILE *l_File=NULL;
	bool l_Return = true;
	l_File = fopen(FileName.c_str(), "rb");
	if (l_File == NULL)
		l_Return = false;
	u_short header=0;
	int c = 0;

	fread(&header, sizeof(short), 1, l_File);
	//fscanf(l_File, "%x", &header);
	if (!0x55ff&header)
	{
		l_Return = false;
		LOGGER->AddNewLog(ELL_ERROR, "%s: Header not found", FileName.c_str());
	}
	u_short l_NumMat = 0;
	fread(&l_NumMat, sizeof(short), 1, l_File);
	//fscanf(l_File, "%d", &l_NumMat);
	int i = 0;
	u_short l_VertexType=0;
	u_short l_NumTextures=0;
	u_short l_NumCaracters = 0;
	char *l_NombreFichero;
	int j = 0;
	C2DTexture *l_Texture = new C2DTexture();
	std::vector<CTexture *> l_TextureVector;
	fread(&l_VertexType, sizeof(short), 1, l_File);
	for (i = 0; i < l_NumMat; ++i)
	{
		fread(&l_NumTextures, sizeof(u_short), 1, l_File);
		//fscanf(l_File, "%u", &l_NumTextures);
		for (j = 0; j < l_NumTextures; ++j)
		{
			fread(&l_NumCaracters, sizeof(u_short), 1, l_File);
			//fscanf(l_File, "%d", &l_NumCaracters);
			//fgets(l_NombreFichero, l_NumCaracters+1, l_File);
			//l_NombreFichero = "";
			l_NombreFichero = (char *)malloc(l_NumCaracters + 1);
		
			fread(l_NombreFichero, sizeof(char), l_NumCaracters, l_File);
			//l_Texture = new C2DTexture();
			l_Texture->Load(l_NombreFichero);
			l_TextureVector.push_back(l_Texture);
			//Es posible que falte reiniciar l_Texture
			free(l_NombreFichero);
			//CHECKED_DELETE(l_Texture);
		}
		if (l_TextureVector.size() != 0)
		{
			m_Textures.push_back(l_TextureVector);
		}
		l_TextureVector.clear();

		//fread(&m_NumVertexs, sizeof(u_short), 1, l_File);
		//fscanf(l_File, "%d", &m_NumVertexs);
		FillRVs(l_File, l_VertexType);
	}
	//m_RVs.push_back();
	u_short l_Footer=0;
	fread(&l_Footer, sizeof(u_short), 1, l_File);
	//fscanf(l_File, "%x", &l_Footer);
	if (l_Footer&0xff55)
	{
		fclose(l_File);
	} else {
		l_Return = false;
		fclose(l_File);
	}
	return l_Return;
}
bool CStaticMesh::ReLoad()
{
	Unload();
	Load(m_FileName);
	return true;
}
void CStaticMesh::Render(CGraphicsManager *GM) const
{
	for (int i=0; i < m_RVs.size(); ++i)
	{
		for (size_t j = 0; j < m_Textures[i].size(); ++j)
			m_Textures[i][j]->Activate(j);
		m_RVs[i]->Render(GM);
	}
}
void CStaticMesh::Unload()
{
	std::vector<CRenderableVertexs*>::iterator it;
	std::vector<CRenderableVertexs*>::const_iterator itEnd=m_RVs.cend();
	for (it = m_RVs.begin(); it != itEnd; ++it)
	{
		CHECKED_DELETE(*it);
	}
	m_RVs.clear();
}

void CStaticMesh::FillRVs(FILE *Fitxer, const u_short VType)
{
	CGraphicsManager *l_GM = CORE->GetGraphicsManager();
	CRenderableVertexs *l_RV = NULL;

	unsigned short l_VertexCount, l_IndexCount;
	unsigned short l_VertexSize = 0;
	
	fread(&l_VertexCount, sizeof(l_VertexCount), 1, Fitxer);

	if (VType == TNORMAL_TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TNORMAL_TEXTURED_VERTEX);
	else if (VType == TTEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TTEXTURED_VERTEX);
	else if (VType == TCOLORED_2TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TCOLORED_2TEXTURED_VERTEX);
	else if (VType == TCOLORED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TCOLORED_VERTEX);
	else if (VType == TNORMAL_COLORED_2TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TNORMAL_COLORED_2TEXTURED_VERTEX);
	else if (VType == TNORMAL_COLORED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TNORMAL_COLORED_VERTEX);
	else if (VType == TCOLORED_TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TCOLORED_TEXTURED_VERTEX);
	else if (VType == TNORMAL_COLORED_TANGENT_BINORMAL_2TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TNORMAL_COLORED_TANGENT_BINORMAL_2TEXTURED_VERTEX);
	else if (VType == T2TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(T2TEXTURED_VERTEX);
	else if (VType == TNORMAL_COLORED_TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TNORMAL_COLORED_TEXTURED_VERTEX);
	else if (VType == TNORMAL_2TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TNORMAL_2TEXTURED_VERTEX);
	else if (VType == TNORMAL_COLORED_TANGENT_BINORMAL_TEXTURED_VERTEX::GetVertexType())
		l_VertexSize = sizeof(TNORMAL_COLORED_TANGENT_BINORMAL_TEXTURED_VERTEX);

	void *l_Vtxs = new char[l_VertexSize*l_VertexCount];

	fread(l_Vtxs, l_VertexSize, l_VertexCount, Fitxer);

	fread(&l_IndexCount, sizeof(l_IndexCount), 1, Fitxer);
	void *l_Indexs = new unsigned short[l_IndexCount];
	fread(l_Indexs, sizeof(unsigned short), l_IndexCount, Fitxer);

		

	if (VType == TNORMAL_TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TNORMAL_TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TTEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TTEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TCOLORED_2TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TCOLORED_2TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TCOLORED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TCOLORED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TNORMAL_COLORED_2TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TNORMAL_COLORED_2TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TNORMAL_COLORED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TNORMAL_COLORED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TCOLORED_TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TCOLORED_TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TNORMAL_COLORED_TANGENT_BINORMAL_2TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TNORMAL_COLORED_TANGENT_BINORMAL_2TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == T2TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<T2TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TNORMAL_COLORED_TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TNORMAL_COLORED_TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TNORMAL_2TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TNORMAL_2TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);
	else if (VType == TNORMAL_COLORED_TANGENT_BINORMAL_TEXTURED_VERTEX::GetVertexType())
		l_RV = new CIndexedVertexs<TNORMAL_COLORED_TANGENT_BINORMAL_TEXTURED_VERTEX>(l_GM, l_Vtxs, l_Indexs, l_VertexCount, l_IndexCount);

	CHECKED_DELETE_ARRAY(l_Vtxs);
	CHECKED_DELETE_ARRAY(l_Indexs);

	if (l_RV != NULL)
		m_RVs.push_back(l_RV);


	
}